package com.advert.system.util;

import com.advert.common.utils.DateUtils;
import com.advert.common.utils.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.WeekFields;
import java.util.*;

public class DateUtil {
    public static String START = " 00:00:00";
    public static String END = " 23:59:59";
    public static String TIME = ":00";


    public static String new_orderNo(String machindId) {

        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String orderNo = "DD" + dateFormat.format(new Date())+RandomGUIDUtil.getRandom() + RandomGUIDUtil.getLastNum();
        if (orderNo.substring(12,16).equals("0000")) {
            try {
                Thread.sleep(1000l);
            } catch (Exception e) {

            }
            orderNo = new_orderNo(machindId);
        }

        return orderNo;
    }
    public static String orderNo(String env,String orderType) {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String orderNo =  "KLTNEW"+ (env.equals("online") ? "ZS" : "CS") +orderType+ dateFormat.format(new Date())+RandomGUIDUtil.getRandom() + RandomGUIDUtil.getLastNum();
        orderNo = orderNo.length() > 32 ? orderNo.substring(0,31) : orderNo;
        return  orderNo;
    }

    public static String orderPrefix(String env,String orderType) {
        return  "KLTNEW"+ (env.equals("online") ? "ZS" : "CS") +orderType;
    }

    public static String new_orderNo1() {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return dateFormat.format(new Date());
    }

    public static String new_orderNo(String orderNo, String SSS) {
        return "DD" + orderNo + SSS + RandomGUIDUtil.getLastNum();
    }

    public static String date(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(date);
    }

    /**
     * @return
     * @Description: 获取当前日期的前一天
     * @ReturnType String
     * @author: liyl
     * @Created 2015年11月13日 下午5:11:14
     */
    public static Date currentBeforeDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return calendar.getTime();
    }

    public static Date setCurrentMonth(Date date,int i) {

        Calendar calendar= Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, i);

        return calendar.getTime();
    }
    //当月首日
    public static String setCurrentMonthfirst(){
        Calendar cale = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        cale.set(Calendar.DAY_OF_MONTH, 1);
        return format.format(cale.getTime());
    }
    /**
     * 将Date类型转换成String类型
     *
     * @param date Date对象
     * @return 形如:"yyyy-MM-dd HH:mm:ss"
     */
    public static String date2String(Date date) {
        return date2String(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 将Date按格式转化成String
     *
     * @param date    Date对象
     * @param pattern 日期类型
     * @return String
     */
    public static String date2String(Date date, String pattern) {
        if (date == null || pattern == null) {
            return null;
        }
        return new SimpleDateFormat(pattern).format(date);
    }


    public static String date2(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        return dateFormat.format(date);
    }

    public static String date3(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("HH时mm分ss秒");
        return dateFormat.format(date);
    }

    public static String date4(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("MM-dd");
        return dateFormat.format(date);
    }

    public static String date5(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm");
        return dateFormat.format(date);
    }

    public static String date6(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(date);
    }

    public static String time(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }

    public static String time(Date date, String format) {
        DateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    public static String time2(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        return date != null ? dateFormat.format(date) : "";
    }

    public static String time3(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return dateFormat.format(date);
    }

    public static String time4(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");
        return dateFormat.format(date);
    }

    public static String time5(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return dateFormat.format(date);
    }

    public static String fileno() {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return dateFormat.format(new Date());
    }

    public static String timelog() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        return dateFormat.format(new Date());
    }

    public static Date parsetime(String timeStr) throws ParseException {
        if (StringUtils.isEmpty(timeStr)) {
            return null;
        }
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.parse(timeStr);
    }

    public static Date parsetime(String timeStr, String format) throws ParseException {
        if (StringUtils.isEmpty(timeStr)) {
            return null;
        }
        DateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.parse(timeStr);
    }

    public static Date parsetime1(String datestr) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss");
        return dateFormat.parse(datestr);
    }

    public static Date parsetime3(String timeStr) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return dateFormat.parse(timeStr);
    }

    public static Date parsetime_start(String timeStr) {
        try {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return dateFormat.parse(timeStr + START);
        } catch (Exception e) {
            // TODO: handle exception
            return null;
        }
    }

    public static Date parsetime_end(String timeStr) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.parse(timeStr + END);
    }

    public static Date parseDateByDefaultFormat(String timeStr) throws ParseException {
        if (StringUtils.isEmpty(timeStr)) return null;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.parse(timeStr);
    }


    public static Date parsetime_hour(String timeStr) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.parse(timeStr + TIME);
    }

    public static String wx_time(String timeStr) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return time(dateFormat.parse(timeStr));
    }

    public static String date2str(Date time) {
        return time != null ? String.valueOf(time.getTime()) : "";
    }

    public static Date str2date(String timeStr) {
        if (StringUtils.isNotEmpty(timeStr)) {
            Date date = new Date();
            date.setTime(Long.valueOf(timeStr).longValue());
            return date;
        }
        return null;
    }

    public static String subdays(Date pDate, Date nDate) {
        long p = pDate.getTime();
        long n = nDate.getTime();
        String sub = String.valueOf((n - p) / 1000L / 60L / 60L / 24L);

        return sub.length() == 1 ? "0" + sub : sub;
    }

    public static String subhours(Date pDate, Date nDate) {
        long p = pDate.getTime();
        long n = nDate.getTime();
        String sub = String.valueOf((n - p) / 1000L / 60L / 60L % 24L);

        return sub.length() == 1 ? "0" + sub : sub;
    }

    public static String subminutes(Date pDate, Date nDate) {
        long p = pDate.getTime();
        long n = nDate.getTime();
        String sub = String.valueOf((n - p) / 1000L / 60L % 60L);

        return sub.length() == 1 ? "0" + sub : sub;
    }

    public static String startTime(Date date) {
        if (date == null) {
            return null;
        }
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd" + START);
        return dateFormat.format(date);
    }

    public static String endTime(Date date) {

        if (date == null) {
            return null;
        }
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd" + END);
        return dateFormat.format(date);
    }

    public static String startPayTime(Date payTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(payTime);
        calendar.add(12, -30);

        return time(calendar.getTime());
    }

    public static String endPayTime(Date payTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(payTime);
        calendar.add(12, 30);

        return time(calendar.getTime());
    }

    public static String startPayTime2(Date updateTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(updateTime);
        calendar.add(12, -2);

        return time(calendar.getTime());
    }

    public static String endPayTime2(Date updateTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(updateTime);
        calendar.add(12, 1);

        return time(calendar.getTime());
    }

    public static String startMonth(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(6, -30);

        return startTime(calendar.getTime());
    }

    public static String startWeek(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(6, -7);

        return startTime(calendar.getTime());
    }

    public static Date subTime(Date time, int sub) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(6, sub);

        return calendar.getTime();
    }

    public static String add1s(String timeStr) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(parsetime3(timeStr));
            calendar.add(13, 1);

            return time3(calendar.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return time3(new Date());
    }

    public static String expireTime(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(12, 30);

        return time3(calendar.getTime());
    }

    public static Date onlineTime(Date updateTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(updateTime);
        calendar.add(12, -5);

        return calendar.getTime();
    }

    public static Date month_one() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(5, 1);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        return calendar.getTime();
    }

    public static String datesub(String time) {
        return time.substring(0, time.indexOf(" "));
    }

    public static String timesub(String time) {
        return time.substring(0, time.lastIndexOf(":"));
    }

    public static String parsetime2(String timeStr) {
        return timeStr + TIME;
    }

    public static boolean afterDate(String timeStr) {
        boolean afterDate = true;
        try {
            Date time = parsetime(timeStr);

            afterDate = time.after(new Date());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return afterDate;
    }

    public static String return_time(String shijian) {
        int year = Integer.valueOf(shijian.substring(0, 2), 16).intValue();
        int month = Integer.valueOf(shijian.substring(2, 4), 16).intValue();
        int day = Integer.valueOf(shijian.substring(4, 6), 16).intValue();
        int hour = Integer.valueOf(shijian.substring(6, 8), 16).intValue();
        int minute = Integer.valueOf(shijian.substring(8, 10), 16).intValue();
        int second = Integer.valueOf(shijian.substring(10, 12), 16).intValue();

        System.out.println(year);
        System.out.println(month);
        System.out.println(day);
        System.out.println(hour);
        System.out.println(minute);
        System.out.println(second);

        return shijian;
    }

    public static String getTimeByMinute(int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, minute);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }



    public static Date str2Date(String time,String format) throws ParseException {
        if (null == format) format = "yyyy-MM-dd HH:mm:ss";
        DateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.parse(time);
    }

    public static Date addOneDay(Date input){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(input);
        calendar.add(Calendar.DAY_OF_MONTH,1);
        return calendar.getTime();
    }

    public static String firstDay(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        return format.format(cale.getTime());
    }

    /**
     * 通过时间秒毫秒数判断两个时间的间隔
     * @param start
     * @param end
     * @return
     */
    public static int differentDaysByMillisecond(Date start,Date end) {
        int days = (int) ((end.getTime() - start.getTime()) / (1000*3600*24));
        return days;
    }

    /**
     * 获取当天0点的日期
     * @param date
     * @return
     */
    public static Date getZeroDate(Date date) throws ParseException {
        return parseDateByDefaultFormat(date(date));
    }


    /**
     * 取得指定日期过 day 天后的日期 (当 day 为负数表示指定日之前);
     * @param date null 表示当天
     * @param day
     * @return
     */
    public static Date nextDay(Date date, int day) {
        Calendar cal = Calendar.getInstance();
        if (date != null) {
            cal.setTime(date);
        }
        cal.add(Calendar.DAY_OF_YEAR, day);
        return cal.getTime();
    }


    /**
     * @return
     */
    public static Date nextMonthFirstDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH,1);
        calendar.add(Calendar.MONTH,1);
        return calendar.getTime();
    }

    public static Date getNow(String format) throws ParseException {
        Date dt=new Date();
        SimpleDateFormat dateFormat=new SimpleDateFormat(format);
        return dateFormat.parse(dateFormat.format(dt));
    }

    /**
     * 通过年份 和 第几周 获取日期 （该周的第一天和下周的第一天）
     * @param year
     * @param week 从1开始
     * @return
     */
    public static List<Date> getDatesByYearAndWeek(int year,int week){
        List<Date> res = new ArrayList<>();
        WeekFields weekFields = WeekFields.of(Locale.getDefault());
        LocalDate ld1 = LocalDate.now()
                .withYear(year)
                .with(weekFields.weekOfYear(), week)
                .with(weekFields.dayOfWeek(), 1);

        LocalDate ld2 = LocalDate.now()
                .withYear(year)
                .with(weekFields.weekOfYear(), week+1)
                .with(weekFields.dayOfWeek(), 1);

        Date dateStart = Date.from(ld1.atStartOfDay(ZoneId.systemDefault()).toInstant());
        Date dateEnd = Date.from(ld2.atStartOfDay(ZoneId.systemDefault()).toInstant());

        res.add(dateStart);
        res.add(dateEnd);


        return res;
    }

    /**
     * 通过年份 和 第几月 获取日期 （该月的第一天和下月的第一天）
     * @param year
     * @param month 从1开始
     * @return
     */
    public static List<Date> getDatesByYearAndMonth(int year,int month){
        List<Date> res = new ArrayList<>();
        LocalDate ld1 = LocalDate.now().withYear(year).withMonth(month).withDayOfMonth(1);
        LocalDate ld2 = LocalDate.now().withYear(year).withMonth(month+1).withDayOfMonth(1);

        Date dateStart = Date.from(ld1.atStartOfDay(ZoneId.systemDefault()).toInstant());
        Date dateEnd = Date.from(ld2.atStartOfDay(ZoneId.systemDefault()).toInstant());

        res.add(dateStart);
        res.add(dateEnd);



        return res;
    }

    /**
     * 根据开始日期和结束日期 获取日期描述
     * @param start
     * @param end
     * @return （10.01至10.09）
     */
    public static String getDescFromDates(Date start,Date end){
        DateFormat dateFormat = new SimpleDateFormat("MM.dd");
        String strStart = dateFormat.format(start);
        String strEnd = dateFormat.format(end);
        return String.format("(%s至%s)",strStart,strEnd);
    }


    public static String deliveryCode(String orderNo) {
        String orderNoCode = "";
        String s1 = orderNo.substring(0, 7);
        String s2 = orderNo.substring(7, 14);
        for (int i = 0; i < 7; i++) {
            int n = Integer.parseInt(s1.substring(i, i + 1)) * 2 + 5;
            String n1 = String.valueOf(n).length() == 2 ? String.valueOf(n).substring(1, 2) : String.valueOf(n);

            int m = Integer.parseInt(n1) + Integer.parseInt(s2.substring(i, i + 1));
            String m1 = String.valueOf(m).length() == 2 ? String.valueOf(m).substring(1, 2) : String.valueOf(m);
            orderNoCode = orderNoCode + m1;
        }
        return orderNoCode;
    }
    /**
     * 获取当前日期
     * @return
     */
    public static String getCurrentDate(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date());
    }
    /**
     * 根据指定格式 获取当前日期
     * @return
     */
    public static String getCurrentDate(String formatDate){
        SimpleDateFormat format = new SimpleDateFormat(formatDate);
        return format.format(new Date());
    }
    /**
     * 获取两个日期之间的所有日期
     *
     * @param startTime
     *            开始日期
     * @param endTime
     *            结束日期
     * @return
     */
    public static List<String> getDays(String startTime, String endTime) {

        // 返回的日期集合
        List<String> days = new ArrayList<String>();

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return days;
    }
    //指定月份首日
    public static String getFixCurrentMonthfirst(Date date,int i){
        Calendar cale = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        cale.add(Calendar.MONTH, i);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        return format.format(cale.getTime());
    }
    public static String getYesterday(int day){
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.DAY_OF_YEAR, day);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(cale.getTime());
    }

    public static Date getLastMonth(Date date,int month){
        Instant instant = date.toInstant();//An instantaneous point on the time-line.(时间线上的一个瞬时点。)
        ZoneId zoneId = ZoneId.systemDefault();//A time-zone ID, such as {@code Europe/Paris}.(时区)
        LocalDateTime now = instant.atZone(zoneId).toLocalDateTime();
        now = now.plusMonths(month);
        ZonedDateTime zdt = now.atZone(zoneId);
        return Date.from(zdt.toInstant());
    }

  /**
   * 当月最后一天
   * @return
   */
  public static String endTimeMonth(){
      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
      //获得实体类
      Calendar ca = Calendar.getInstance();
      //设置最后一天
      ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
      //最后一天格式化
      return format.format(ca.getTime());
    }
    public static void main(String args[]) {

      String orderNo = "KLTNEWZSACT20240711144309736B9F";
      System.out.print(new Date().getTime() - DateUtils.parseDate(orderNo.substring(11,25)).getTime() > 10 * 60 * 1000l);
    }
    public static String startTime2(Date date) {
      if (date == null) {
        return null;
      }
      DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
      return dateFormat.format(date);
    }

}
